class PhoneUtility {
	constructor() {
		this.Intent = plus.android.importClass("android.content.Intent");
		this.Uri = plus.android.importClass("android.net.Uri");
		this.mainActivity = plus.android.runtimeMainActivity();
		this.CallLog = plus.android.importClass("android.provider.CallLog");
	}

	init() {
		plus.android.requestPermissions([
			'android.permission.READ_CALL_LOG',
			'android.permission.WRITE_CALL_LOG',
			'android.permission.CALL_PHONE'
		], res => {
			if (res.granted.length > 0) {
				console.log('已授权');
				// 可以在这里调用其他方法，比如获取通话记录
			} else {
				uni.showToast({
					title: "通话记录权限未开启",
					icon: 'none'
				});
				console.log('请先授权');
			}
		});
	}


	//拨打电话
	callPhone(data) {
		const uri = this.Uri.parse("tel:" + data);
		const callIntent = new this.Intent("android.intent.action.CALL", uri);
		this.mainActivity.startActivity(callIntent);
	}
	//调取通话记录
	callLog() {
		const main = this.mainActivity;
		const obj = main.getContentResolver();
		plus.android.importClass(obj);

		const day2 = new Date();
		day2.setTime(day2.getTime());
		const s2 = `${day2.getFullYear()}-${day2.getMonth() + 1}-${day2.getDate()}`;

		const day3 = new Date();
		day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000);
		const s3 = `${day3.getFullYear()}-${day3.getMonth() + 1}-${day3.getDate()}`;

		const selection = {
			mobile: '',
			startTime: new Date(s2).getTime(),
			endTime: new Date(s3).getTime(),
			type: ''
		};

		let sql = '';
		let keyword = '';

		for (const item in selection) {
			if (selection[item] !== '') {
				switch (item) {
					case 'mobile':
						sql = sql ? `${sql} and ` : '';
						sql += `${this.CallLog.Calls.NUMBER} like '%${selection[item]}%'`;
						keyword = selection[item];
						break;
					case 'startTime':
						sql = sql ? `${sql} and ` : '';
						sql += `${this.CallLog.Calls.DATE}>=${selection[item]}`;
						break;
					case 'endTime':
						sql = sql ? `${sql} and ` : '';
						sql += `${this.CallLog.Calls.DATE}<=${selection[item]}`;
						break;
					case 'type':
						sql = sql ? `${sql} and ` : '';
						sql += `${this.CallLog.Calls.TYPE}=${selection[item]}`;
						break;
				}
			}
		}

		sql = sql || null;
		const cursor = main.getContentResolver().query(
			this.CallLog.Calls.CONTENT_URI,
			null,
			sql,
			null,
			null
		);

		plus.android.importClass(cursor);
		const content = [];
		let count = 0;

		if (cursor.moveToFirst()) {
			do {
				count++;
				const number = cursor.getString(cursor.getColumnIndex(this.CallLog.Calls.NUMBER));
				let type;
				switch (parseInt(cursor.getString(cursor.getColumnIndex(this.CallLog.Calls.TYPE)), 10)) {
					case this.CallLog.Calls.INCOMING_TYPE:
						type = "呼入";
						break;
					case this.CallLog.Calls.OUTGOING_TYPE:
						type = "呼出";
						break;
					case this.CallLog.Calls.MISSED_TYPE:
						type = "未接";
						break;
					default:
						type = "挂断";
						break;
				}
				const date = new Date(parseInt(cursor.getString(cursor.getColumnIndexOrThrow(this.CallLog.Calls
					.DATE)), 10));
				const name = cursor.getString(cursor.getColumnIndexOrThrow(this.CallLog.Calls.CACHED_NAME));
				const numberLocation = cursor.getString(cursor.getColumnIndex(this.CallLog.Calls
					.GEOCODED_LOCATION));
				const duration = cursor.getString(cursor.getColumnIndexOrThrow(this.CallLog.Calls.DURATION));

				content.push({
					name: name,
					mobile: number,
					numberLocation: numberLocation,
					callTime: 0,
					talkTime: duration,
					type: type
				});

				if (count > 50) break;
			} while (cursor.moveToNext());
		}
		console.log(content[content.length - 1]);
	}

	PhoneState(data) {
		const Contexttest = plus.android.importClass("android.content.Context");
		const telephonyManager = plus.android.importClass("android.telephony.TelephonyManager");
		const telManager = this.mainActivity.getSystemService(Contexttest.TELEPHONY_SERVICE);
		const receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
			onReceive: (Contexttest, intent) => {
				plus.android.importClass(intent);
				console.log(intent.getAction());
				const phonetype = telManager.getCallState();
				const phoneNumber = intent.getStringExtra(telephonyManager.EXTRA_INCOMING_NUMBER);
				console.log("phonetype:" + phonetype); //电话状态 0->空闲状态 1->振铃状态 2->通话存在  
				console.log(phoneNumber, '电话号码');
				switch (phonetype) {
					case 0:
						// this.callLog();//通话记录
						console.log('aaa');
						break;
					case 1:
						console.log('bbb');
						break;
					case 2:
						console.log('ccc');
						break;
				}
			}
		});
		const IntentFilter = plus.android.importClass('android.content.IntentFilter');
		const filter = new IntentFilter();
		filter.addAction(telephonyManager.ACTION_PHONE_STATE_CHANGED);
		this.mainActivity.registerReceiver(receiver, filter);
	}
}

export default PhoneUtility;
